#ifndef _TALK_SESSION_STR_STRCALLMANAGER_H_
#define _TALK_SESSION_STR_STRCALLMANAGER_H_

#include "talk/examples/crossfirewall/cfcall.h"
#include "talk/examples/crossfirewall/cmdconsole.h"
#include "talk/examples/crossfirewall/cfsessionclient.h"

#include "talk/base/helpers.h"
#include "talk/p2p/base/sessionmanager.h"
#include "talk/p2p/client/sessionmanagertask.h"
#include "talk/p2p/client/basicportallocator.h"

using namespace talk_base;

#define CMD_CALL_STATE_CHANGE        200
#define CMD_CALL_DO_MAKECALL         201
#define CMD_CALL_DO_HANGEUPCALL      202
#define CMD_CALL_DO_DESTROYCALL      203
#define CMD_CALL_DO_HANGEUPCALL_ALL  204
#define CMD_CALL_DO_REQUESTIPLIST    205

namespace cricket {

	struct CallState : public MessageData {
		std::string jid_; int state_;
		CallState(std::string jid, int state) : jid_(jid), state_(state) { }
	};

	class CFCallManager : public CoreCmd, public sigslot::has_slots<>
	{
	public:
		CFCallManager(const buzz::Jid& local_jid, buzz::XmppClient* xmpp_client, bool bhttpallocator);
		~CFCallManager();

		void MakeCall(std::string remote_jid);
		void HangupCall(std::string remote_jid);
		void DestroyCall(std::string remote_jid);
		void HangupCallAll();

		void SetCmdWayToCore(Thread* cmd_thread, CmdConsole* cmd_console);
		void SetHttpAllocatorHosts(const std::string & relay_token, const std::vector<std::string> &relay_addresses,
			const std::vector<talk_base::SocketAddress> &stun_addresses) {
				cfsession_client_->SetHttpAllocatorHosts(relay_token, relay_addresses, stun_addresses);
		}

	private:
		CFCall* CreateCall(std::string remote_jid);
		
		void OnCallStateChange(CFCall* call, std::string remote_jid, int state);
		void OnSessionIncoming(Session* session);
		void OnSessionDestroy(Session* session);

	public:
		sigslot::signal3<CFCall*, std::string, int> SignalCallStateChange;

	private:
		typedef std::map<std::string, CFCall*> CallMap;
		CallMap           call_map_;
		
		buzz::Jid         local_jid_;
		CriticalSection   callmanager_crit_;

		Thread*           cmd_thread_;
		CmdConsole*       cmd_console_;
		CFSessionClient*  cfsession_client_;
	};
}

#endif
